﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SEO.Model;
using System.Data.SQLite;

namespace SEO.DAL
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 关键词表
    /// 完成日期：2011年10月16日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class ESKeywordsDAL
    {

        /// <summary>
        /// 获取ESKeywords表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<ESKeywords> GetAllESKeywordsDAL()
        {
            List<ESKeywords> list = new List<ESKeywords>();
            string strSql = "select * from Keywords";
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    ESKeywords ks = new ESKeywords();
                    ks.KeyID = int.Parse(dr["key_id"].ToString());
                    ks.KeyName = dr["key_name"].ToString();
                    ks.CatchStatus = dr["catch_status"].ToString();
                    list.Add(ks);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定ESKeywords -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取ESKeywords表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<ESKeywords> GetESKeywordsByWhereDAL(string strSql)
        {
            List<ESKeywords> list = new List<ESKeywords>();
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    ESKeywords ks = new ESKeywords();
                    ks.KeyID = int.Parse(dr["key_id"].ToString());
                    ks.KeyName = dr["key_name"].ToString();
                    ks.CatchStatus = dr["catch_status"].ToString();
                    list.Add(ks);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定SQL语句获取ESKeywords表中数据 -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往ESKeywords表中插入数据
        /// <param name="ks">实例化ESKeywords类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertESKeywordsDAL(SEO.Model.ESKeywords ks)
        {
            int iNum = 0;
            string strSql = "insert into Keywords(key_id,key_name) " +
             " values(" + ks.KeyID + ",'" + ks.KeyName + "')";
            try
            {
                iNum = SEO.DBSqlHelp.ESDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("往ESKeywords表中插入数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除ESKeywords表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteESKeywordsDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.ESDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据指定条件删除ESKeywords表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新ESKeywords表中数据
        /// </summary>
        /// <param name="ks">实例化ESKeywords类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateESKeywordsDAL(SEO.Model.ESKeywords ks)
        {
            int iNum = 0;
            string strSql = "update Keywords set key_name=" + ks.KeyName + " where key_id=" + ks.KeyID;
            try
            {
                iNum = SEO.DBSqlHelp.ESDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新ESKeywords表中数据 -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表ESKeywords到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindESKeywordsDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataAdapter da = new SQLiteDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "Keywords");
                ds.Tables["Keywords"].Columns.Add("ViewStatus", typeof(string));
                for (int i = 0; i < ds.Tables["Keywords"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["Keywords"].Rows[i];
                    string strStatus = dr["catch_status"].ToString();
                    if (strStatus == "0")
                    {
                        dr["ViewStatus"] = "未采集";
                    }
                    else
                    {
                        dr["ViewStatus"] = "已采集";
                    }
                }
                dgv.DataSource = ds.Tables["Keywords"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定ESKeywords -> " + ex.Message);
            }
        }

        /// <summary>
        /// 获取表中最大ID，并自增1
        /// </summary>
        /// <returns>返回表中最大ID，并自增1</returns>
        public int GetAutoAddNum()
        {
            int iNum = 0;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "SELECT key_id FROM Keywords ORDER BY key_id DESC limit 0,1";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    iNum = int.Parse(dr["key_id"].ToString()) + 1;
                }
                else
                {
                    iNum = 1;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("获取表中最大ID，并自增1 -> " + ex.Message);
            }

            return iNum;
        }

        /// <summary>
        /// 检测是否已存在数据库中，屏蔽已存在的关键词
        /// </summary>
        /// <param name="strKeyword">关键词</param>
        /// <returns>返回 True or False</returns>
        public bool CheckEmailAddressIsExist(string strKeyword)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select key_id from Keywords where key_name='" + strKeyword + "'";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    bValue = true;
                }
                else
                {
                    bValue = false;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("检测是否已存在数据库中 -> " + ex.Message);
            }

            return bValue;
        }

        /// <summary>
        /// 更新关键词状态
        /// </summary>
        /// <param name="iKeyID">关键词ID</param>
        /// <returns></returns>
        public bool UpdateKeywordsStatus(int iKeyID)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "update Keywords set catch_status=1 where key_id=" + iKeyID;
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                cm.ExecuteNonQuery();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新关键词状态 -> " + ex.Message);
            }

            return bValue;
        }

        /// <summary>
        /// 更新关键词状态
        /// </summary>
        /// <param name="strKeyword">关键词</param>
        /// <returns></returns>
        public bool UpdateKeywordsStatus(string strKeyword)
        {
            bool bValue = false;
            try
            {
                SQLiteConnection cn = new SQLiteConnection(SEO.DBSqlHelp.ESDBHelper.ConnectionEmailSearch());
                cn.SetPassword(SEO.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "update Keywords set catch_status=1 where key_name='" + strKeyword + "'";
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                cm.ExecuteNonQuery();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("更新关键词状态 -> " + ex.Message);
            }

            return bValue;
        }

    }
}
